import cTitle from "components/title";
export default {
  data() {
    return {
      userAvatar: "",
      userName: "",
      vipId: "",

      mailLanguage: "",
      mail_distrbution_name: "",
      mail_commission: "",

      toi: this.fun.getKeyByI(),

      first_level: 0,
      second_level: 0,
      third_level: 0,

      expect: 0,
      unliquidated: 0,
      liquidated: 0,
      not_yet_withdrawed: 0,
      withdrawed: 0,
      manage: 0,

      count: 0,

      invalid: 0,
      mail_commission_amount: ""
    };
  },

  async activated() {
    this.toi = this.fun.getKeyByI();
    this.userAvatar = "";
    this.userName = "";
    this.vipId = "";

    this.first_level = 0;
    this.second_level = 0;
    this.third_level = 0;

    this.expect = 0;
    this.unliquidated = 0;
    this.liquidated = 0;
    this.not_yet_withdrawed = 0;
    this.withdrawed = 0;

    this.count = 0;

    this.setUserInfo(); //设置用户信息
    this.initMailLanguage();
  },
  mounted() {
    this.toi = this.fun.getKeyByI();
  },

  methods: {
    initMailLanguage() {
      this.mailLanguage = this.fun.initMailLanguage();
      this.fun.setWXTitle(this.fun.setMailLanguage("分销商", this.mailLanguage.commission ? this.mailLanguage.commission.agent : ""));
      this.mail_distrbution_name = this.fun.setMailLanguage("分销等级", this.mailLanguage.commission.level_name);
      this.mail_commission = this.fun.setMailLanguage("分销", this.mailLanguage.commission.commission);
      this.mail_commission_amount = this.fun.setMailLanguage("佣金", this.mailLanguage.commission.commission_amount);
    },

    msg() {
      this.$dialog.alert({ message: `默认${this.mail_commission}比例,因商品、供应商可以独立设置佣金，您最终获得的佣金可能与此比例不同` });
    },

    //获取数据
    getLevelData() {
      let that = this;
      let json = { i: this.fun.getKeyByI(), type: this.fun.getTyep() };
      $http.get("plugin.commission.api.commission.get-agent-level", json).then(
        function(response) {
          console.log(response);
          if (response.result == 1) {
            that.setDistributionInfo(response.data);
            that.setDistributionScaleInfo(response.data);
          } //else {

          // }
        },
        function(response) {
          console.log(response);
        }
      );
    },

    //获取分销佣金
    getDistributionCommissionData() {
      let that = this;
      let json = { i: this.fun.getKeyByI(), type: this.fun.getTyep() };
      $http.get("plugin.commission.api.commission.get-commission", json).then(
        function(response) {
          console.log(response);
          if (response.result == 1) {
            that.setDistributionCommissionData(response.data);
            that.invalid = response.data.invalid;
          } //else {

          //}
        },
        function(response) {
          console.log(response);
        }
      );
    },
    //获取我的下线总数
    getAgentCountData() {
      let that = this;
      let json = { i: this.fun.getKeyByI(), type: this.fun.getTyep() };
      $http.get("member.member.getMyAgentCount", json).then(
        function(response) {
          console.log(response);
          if (response.result == 1) {
            that.setAgentCountData(response.data);
          } // else {

          //}
        },
        function(response) {
          console.log(response);
        }
      );
    },

    //设置用户信息
    setUserInfo() {
      var that = this;
      let json = { i: this.fun.getKeyByI(), type: this.fun.getTyep() };
      $http.get("member.member.getUserInfo", json).then(
        response => {
          if (response.result == 1) {
            if (!response.data.is_agent) {
              this.$dialog
                .confirm({ message: "请先申请成为推广员" })
                .then(() => {
                  this.$router.replace(this.fun.getUrl("ExtensionApply", {}));
                })
                .catch(() => {
                  this.$router.go(-1);
                });
              return;
            }
            that.userName = response.data.nickname;
            that.vipId = response.data.uid;
            that.userAvatar = response.data.avatar;
            this.getDistributionCommissionData(); //获取分销佣金
            this.getAgentCountData(); //获取我的下线总数
            this.getLevelData(); //获取用户分销商等级
          }
        },
        function(response) {
          // error callback
        }
      );

      // this.userName = this.$store.state.memberstate.member_model.nickname;
      // this.vipId = this.$store.state.memberstate.member_model.uid;
      // this.userAvatar = this.$store.state.memberstate.member_model.avatar;
      // console.log(this.$store.state.memberstate.member_model.nickname);
    },

    //设置分销商 基本信息
    setDistributionInfo(data) {
      this.distrbution_name = data.name;
      this.distrbution_tiem = data.created_at;
    },

    //设置分销比例信息
    setDistributionScaleInfo(data) {
      this.first_level = data.first_level;
      this.second_level = data.second_level;
      this.third_level = data.third_level;
    },

    //设置分销佣金

    setDistributionCommissionData(data) {
      this.expect = data.expect; //预计佣金
      this.unliquidated = data.unliquidated; //未结算佣金
      this.liquidated = data.liquidated; //已结算佣金
      this.not_yet_withdrawed = data.not_yet_withdrawed; //未提现佣金
      this.withdrawed = data.withdrawed; // 已提现佣金
      this.manage = data.manage; //这个好像没用到
      this.is_manage = data.is_manage == "1" ? true : false; //无效佣金
    },

    //设置我的下线总数
    setAgentCountData(data) {
      this.count = data.count;
    }
  },
  components: { cTitle }
};
